﻿using Common.Infrastructure.Utils;
using Common.RunModels.ES;
using Log.Api.LogModels;
using Log.Api.Models;
using Log.Data.Models;
using System.Threading.Tasks;

namespace Log.Api.Utils
{
    public static class ElasticsearchUtils
    {
        public static async Task SaveCapErrorLogAsync(string url, string index, CapErrorLogModel model)
        {
            var sts = new ElasticsearchCapErrorLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                capId = model.CapId,
                capType = model.CapType,
                groupName = model.GroupName,
                message = model.Message,
                msgObj = model.MsgObj,
                name = model.Name,
            };

            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        public static async Task SaveDownExtLogAsync(string url, string index, DownExtLogModel model)
        {
            var sts = new ElasticsearchDownExtLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                exeSecond = model.ExeSecond,
                api = model.Api,
                msg = model.Msg,
                paramObj = model.ParamObj,
                resultObj = model.ResultObj,
                title = model.Title,
            };
            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        public static async Task SaveSyncLogAsync(string url, string index, SyncLogModel model)
        {
            var sts = new ElasticsearchSyncLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                syncCode = model.SyncCode,
                syncId = model.SyncId,
                syncName = model.SyncName,
                exeSecond = model.ExeSecond,
                addC = model.AddC,
                api = model.Api,
                count = model.Count,
                delC = model.DelC,
                msg = model.Msg,
                updateC = model.UpdateC,
            };

            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        public static async Task SaveSysLogAsync(string url, string index, SysLogModel model)
        {
            var sts = new ElasticsearchSysLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                costTick = model.CostTick,
                cTTick = model.CTTick,
                logKey = model.LogKey,
                logMsg = model.LogMsg,
                logMsgParamObj = model.LogMsgParamObj,
                msg = model.Msg,
                msgTrace = model.MsgTrace,
                paramObj = model.ParamObj,
                type = model.Type,
            };
            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        public static async Task SavePackScanLogAsync(string url, string index, PackScanLogModel model)
        {
            var sts = new ElasticsearchPackScanLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                scanType = model.ScanType,
                scanValue = model.ScanValue,
                scanValue2 = model.ScanValue2,
                scanValue3 = model.ScanValue3,
                standValue = model.StandValue,
                standValue2 = model.StandValue2,
                standValue3 = model.StandValue3,
                Operator = model.Operator,
                orderNo = model.OrderNo,
                result = model.Result,
                result2 = model.Result2,
                result3 = model.Result3,
                time = model.Time,
            };
            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        /// <summary>
        /// 保存设备参数记录
        /// </summary>
        /// <param name="url"></param>
        /// <param name="index"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task SaveEquipmentParamLogAsync(string url, string index, EquipmentParamLogModel model)
        {
            var sts = new ElasticsearchEquipmentParamLogModel()
            {
                id = model.Id,
                cT = model.CT.ToString("o"),
                apiKey = model.ApiKey,
                serviceCode = model.ServiceCode,
                orgId = model.OrgId,
                workCode = model.WorkCode,
                lineCode = model.LineCode,
                lineName = model.LineName,
                workstageCode = model.WorkstageCode,
                equipmentCode = model.EquipmentCode,
                equipmentType = model.EquipmentType,
                paramCode = model.ParamCode,
                paramName = model.ParamName,
                paramValue = model.ParamValue,
                operateUserName = model.OperateUserName
            };
            await DoSaveLogByIndexAsync(url, index, model.Id, sts);
        }

        private static async Task DoSaveLogByIndexAsync(string url, string index, string id, object obj)
        {
            string basicAuth = null;
            if (url.Contains("@"))
            {
                var pst = url.Split("@")[0];
                pst = pst.Replace("http://", string.Empty);
                basicAuth = pst;
            }

            try
            {
                await WebApiUtils.PutByResponseByBasicAuth($"{url}/{index}/_doc/{id}", obj, basicAuth);
            }
            catch 
            { 
            }
        }
    }
}
